<?php

namespace App\Command\ResumeParsing\Service\ParsingSpecial;

use EchoPHP\Facades\Log as Log;
use PhpOffice\PhpSpreadsheet;

class ParsingCustomExcel extends ParsingSpecialExcel implements PreprocessorExcel {

    protected $titlePatternTemplate = ['range' => 'A2:F2', 'core' => 'A2', 'xfIndex' => null];

    public function __construct($helper) {
        parent::__construct($helper);

        $this->get['id'] = $this->tagNotExist;

        $this->get['time'] = $this->tagNotExist;

        $this->get['name'] = function () {
            return $this->data['个人信息']['姓名'] ?? '';
        };

        $this->get['sex'] = function () {
            return $this->data['个人信息']['性别'] ?? '';
        };

        $this->get['nation'] = function () {
            return $this->data['个人信息']['民族'] ?? '';
        };

        $this->get['identitycard'] = function () {
            return $this->data['个人信息']['身份证'] ?? '';
        };

        $this->get['mobile'] = function () {
            return $this->data['个人信息']['手机'] ?? '';
        };

        $this->get['email'] = function () {
            return $this->data['个人信息']['E-mail'] ?? '';
        };

        $this->get['age'] = function () {
            return $this->data['个人信息']['年龄'] ?? '';
        };

        $this->get['married'] = function () {
            return $this->data['个人信息']['婚姻状态'] ?? '';
        };

        $this->get['politic'] = function () {
            return $this->data['个人信息']['政治面貌'] ?? '';
        };

        $this->get['degree'] = function () {
            return $this->data['个人信息']['学历'] ?? '';
        };

        $this->get['industry'] = function () {
            return $this->data['求职意向']['行业'] ?? '';
        };

        $this->get['position'] = function () {
            $strPosition = '';
            if (isset($this->data['求职意向']['职位'])) {
                $strPosition .= trim($this->data['求职意向']['职位']);
            }
            if (isset($this->data['求职意向']['职能'])) {
                $strPosition .= ' ' . trim($this->data['求职意向']['职能']);
            }
            return trim($strPosition);
        };

        $this->get['career'] = function () {
            return $this->data['求职意向']['职业'] ?? '';
        };

        $this->get['major'] = function () {
            return $this->data['个人信息']['专业'] ?? '';
        };

        $this->get['birthday'] = function () {
            return $this->data['个人信息']['出生日期'] ?? '';
        };

        $this->get['workyear'] = function () {
            return $this->data['个人信息']['工作年限'] ?? '';
        };

        $this->get['workarea'] = function () {
            return $this->data['个人信息']['现居住地'] ?? '';
        };

        $this->get['expectarea'] = function () {
            return $this->data['求职意向']['期望工作地'] ?? '';
        };

        $this->get['area'] = function () {
            return $this->data['个人信息']['户口'] ?? '';
        };

        $this->get['salary'] = function () {
            $resultText = $this->data['个人信息']['目前月薪'] ?? '';
            $salary = $this->func->parseSalary($resultText);
            if (count($salary)) {
                $this->helper->resumeInfo['salaryfrom'] = $salary['monthly']['begin'];
                $this->helper->resumeInfo['salaryend'] = $salary['monthly']['end'];
            }
            return $resultText;
        };

        $this->get['expectsalary'] = function () {
            $resultText = $this->data['求职意向']['期望月薪'] ?? '';
            $expectSalary = $this->func->parseSalary($resultText);
            if (count($expectSalary)) {
                $this->helper->resumeInfo['expectsalaryfrom'] = $expectSalary['monthly']['begin'];
                $this->helper->resumeInfo['expectsalaryend'] = $expectSalary['monthly']['end'];
            }
            return $resultText;
        };

        $this->get['jobterm'] = function () {
            return $this->data['求职意向']['工作类型'] ?? '';
        };

        $this->get['current_situation'] = function () {
            return $this->data['求职意向']['求职状态'] ?? '';
        };

        $this->get['entrytime'] = function () {
            return $this->data['求职意向']['到岗时间'] ?? '';
        };

        $this->get['school'] = function () {
            return $this->data['个人信息']['毕业院校'] ?? '';
        };

        $this->get['nofocuse'] = function () {
            return $this->data['附加信息']['勿推企业'] ?? '';
        };

        $this->get['workinfo'] = function () {
            $data = [];
            foreach ($this->data['工作经历'] as $item) {
                if (empty($item['时间'])) continue;
                $time = $this->func->parseTimespan($item['时间']);
                $data[] = [
                    'time' => $item['时间'],
                    'time_from' => $time['begin'],
                    'time_end' => $time['end'],
                    'company' => $item['公司'],
                    'industry' => $item['行业'] ?? '',
                    'duty' => $item['职位'] ?? '',
                    'salary' => $item['薪资'] ?? '',
                    'comment' => $item['工作描述'] ?? '',
                ];
            }
            return $data;
        };

        $this->get['projectinfo'] = function () {
            $data = [];
            foreach ($this->data['项目经历'] as $item) {
                if (empty($item['时间'])) continue;
                $time = $this->func->parseTimespan($item['时间']);
                $data[] = [
                    'time' => $item['时间'],
                    'time_from' => $time['begin'],
                    'time_end' => $time['end'],
                    'project' => $item['项目'],
                    'company' => $item['公司'] ?? '',
                    'duty' => $item['职位'] ?? '',
                    'comment' => $item['项目描述'] ?? '',
                ];
            }
            return $data;
        };

        $this->get['educationinfo'] = function () {
            $data = [];
            foreach ($this->data['教育经历'] as $item) {
                if (empty($item['时间'])) continue;
                $time = $this->func->parseTimespan($item['时间']);
                $data[] = [
                    'time' => $item['时间'],
                    'time_from' => $time['begin'],
                    'time_end' => $time['end'],
                    'school' => $item['学校'],
                    'major' => $item['专业'] ?? '',
                    'degree' => $item['学历'] ?? '',
                    'comment' => $item['备注'] ?? '',
                ];
            }
            return $data;
        };

        $this->get['skillinfo'] = function () {
            return $this->data['附加信息']['技能特长'] ?? '';
        };

        $this->get['awardinfo'] = function () {
            return $this->data['附加信息']['获奖情况'] ?? '';
        };

        $this->get['commentinfo'] = function () {
            return $this->data['求职意向']['自我评价'] ?? '';
        };

        $this->get['otherinfo'] = function () {
            return $this->data['附加信息']['其他'] ?? '';
        };
    }

    protected function matchTitleStyle() {
        if (empty($cell = $this->sheet->getCellByColumnAndRow($this->titlePattern['core'][0], $this->titlePattern['core'][1], false))) return false;
        if (empty($style = $cell->getStyle())) return false;
        if ($style->getFill()->getStartColor()->getARGB() != 'FF99CCFF') return false;
        if ($style->getFill()->getFillType() != PhpSpreadsheet\Style\Fill::FILL_SOLID) return false;
        if ($style->getFont()->getSize() != 11) return false;
        if ($style->getFont()->getName() != '宋体') return false;
        if ($style->getBorders()->getLeft()->getBorderStyle() != PhpSpreadsheet\Style\Border::BORDER_NONE) return false;
        return $cell->getXfIndex();
    }

    protected function test() {
        if ($this->sheet->getCellByColumnAndRow(1, 1)->getValue() != '简历信息') return false;
        return parent::test();
    }

    protected function onParsed() {
        $this->explode();
        foreach (['个人信息', '求职意向', '附加信息'] as $title) {
            $this->data[$title] = $this->pairRowData($this->data[$title]);
        }
        foreach (['工作经历', '项目经历', '教育经历'] as $title) {
            $this->data[$title] = $this->assignColumnData($this->data[$title]);
        }
    }

    /**
     * Excel文档预处理方法，在此方法中，直接对$workbook对象进行必要的修改。
     * @param PhpSpreadsheet\Spreadsheet $workbook 待处理的Excel文档对象
     * @return bool 若Excel格式不匹配当前指定渠道，则返回false；否则返回true。
     */
    public function preprocess(PhpSpreadsheet\Spreadsheet $workbook) {
        try {
            if ($workbook->getSheetCount() < 2) return false;
            $dictSheet = $workbook->getSheet(0);
            if ($dictSheet->getTitle() != '下拉选项配置') return false;
            if ($dictSheet->getCell('A1')->getValue() != '下拉选项配置页') return false;
            if (!$this->canParse($workbook->getSheet(1))) return false;

            $workbook->removeSheetByIndex(0);
            foreach ($workbook->getWorksheetIterator() as $worksheet) {
                try {
                    if ($worksheet->getCell('A3')->getValue() != '姓名') continue;
                    if (empty($name = $worksheet->getCell('B3')->getValue())) continue;
                    $worksheet->setTitle($name);
                } catch (\Exception $e) {
                    Log::error(e_dump($e), ['sheetName' => $worksheet->getTitle()], 'cmd');
                }
            }
            return true;
        } catch (\Exception $e) {
            Log::error(e_dump($e), [], 'cmd');
            return false;
        }
    }
}
